var $grid = $('#bt_packg_grid');
var viewDialog=$('#dd');

context.ready = function() {
    $grid.datagrid({
        fit: true,
        border: true,
        // width:'95%',
        // height:'500',
        nowrap: true,
        autoRowHeight: true,
        striped: true,
        collapsible:true,
        sortName: 'fhdate desc,BOXNO',
        sortOrder: '',
        idField:'ID',
        url: _ROOT_ + '/packg/getData',
        pagination: true,
        columns: [[
                {field: 'ck',checkbox: true}, 
                {field: 'PO', title: '订单号',  align: 'center'},
                {field: 'PRODUCT', title: '品名',  align: 'center'},
                {field: 'PACKAGE', title: '封装',  align: 'center', sortable: true},
                {field: 'LOTNO', title: '批号', align: 'center', sortable: true},
                {field: 'OQTY', title: '总数',  align: 'center'},
                {field: 'qty', title: '数量',  align: 'center'},
                {field: 'roll', title: '盘数',  align: 'center'},
                {field: 'BOXV', title: '体积',  align: 'center'},
                {field: 'BOXNO', title: '箱号',  align: 'center', sortable: true},
                {field: '封装', title: '封装',  align: 'center'},
                {field: 'fhdate', title: '发货时间',  width: 70,  align: 'center', sortable: true},
                {field: 'Addre', title: '地址',  align: 'center', sortable: true},
                {field: '箱型', title: '箱型',  align: 'center'},
                {field: 'other', title: 'other',  align: 'center'},
                {field: '标准遍盘', title: '标准遍盘',  align: 'center'},
                {field: 'HW', title: 'HW',  align: 'center'},
                {field: 'ID', title: '操作',  align: 'center', formatter: function(value) {
                        return '<span title="编辑" class="img-btn icon-edit" ID=' + value + '></span>';
                    }}
            ]],
        toolbar: [{
                text: '新增',
                iconCls: 'icon-add',
                handler: context.addView
            }, {
                text: '删除',
                iconCls: 'icon-remove',
                handler: context.doDelete
            }],
    });

    $('#bt_packg_search_btn').click(function() {
        var map = $('#bt_packg_search_from').toJson();
        for (var i = map.length - 1; i >= 0; i--) {
           alert(i.map[i]);
        };

        $grid.datagrid('load', $('#bt_packg_search_from').toJson());
    });
};

context.addView = function() {
    viewDialog.dialog({
        title: '新增订单',
        href: _ROOT_ + '/packg/toadd',
        width: 550,
        height: 570,
        maximizable: true,
        resizable: true,
        bodyStyle: {overflow: 'hidden'},
        buttons: [{
                text: '提交',
                handler: context.doSubmit
            }]
    });
};

context.updateView = function(id) {
    viewDialog.dialog({
        title: '编辑拼箱单',
        href: _ROOT_ + '/packg/toUpdate?ID=' + id,
        width: '300',
        height: '500',
        maximizable: true,
        resizable: true,
        bodyStyle: {overflow: 'hidden'},
        buttons: [{
                text: '提交',
                handler: context.doSubmit
            }]
    });
};

context.doDelete = function() {
    var checked = $grid.datagrid('getChecked');
    if (checked && checked.length > 0) {
        $.confirm('确认删除？', function(r) {
            if (r) {
                var ids = [];
                $.each(checked, function() {
                    ids.push(this.uid);
                });
                $.post(_ROOT_ + '/packg/doDelete', {ids: ids.join(',')}, function(rsp) {
                    if (rsp.status) {
                        $grid.datagrid('reload');
                    } else {
                        $.alert(rsp.msg);
                    }
                }, 'JSON');
            }
        });
    }
};

context.doSubmit = function() {
    $bt_packg_from = $('#bt_packg_from');
    if ($bt_packg_from.form('validate')) {
        $.post(_ROOT_ + '/packg/doSave', $bt_packg_from.toJson(), function(rsp) {
            if (rsp.status) {
                $grid.datagrid('reload');
                viewDialog.dialog('close');
            } else {
                $.alert(rsp.msg);
            }
        }, "JSON");
    }
};